package com.itxiuyixiu.volunteer.service;

import com.itxiuyixiu.entity.enums.ActivityState;
import com.itxiuyixiu.entity.mapper.ActivityReaderMapper;
import com.itxiuyixiu.entity.mapper.ActivityWriterMapper;
import com.itxiuyixiu.entity.model.Activity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author 黄磊
 * @date 2020/5/4
 */
@Service
public class TeacherService {
    @Resource
    public ActivityWriterMapper activityWriterMapper;
    @Resource
    public ActivityReaderMapper activityReaderMapper;

    /**
     * 查询所有的老师未审核的活动（不包括审核失败的）
     *
     * @return 未审核的活动
     */
    public List<Activity> findAllCheckingActivity() {
        return activityReaderMapper.findAllActivity().stream()
                .filter(e -> e.getActivityState().compare(ActivityState.IN_PROGRESS_TEACHER) < 0)
                .filter(e -> !e.getActivityState().equals(ActivityState.FAILED))
                .collect(Collectors.toList());
    }

    /**
     * 审核活动
     *
     * @param activityId    要更改状态的活动的id
     * @param checkedResult true - 通过审核， false未通过审核
     * @return 审核结果 {
     * 0 - 成功修改活动状态为审核成功，
     * 1 - 要修改状态的活动不存在，
     * 2 - 成功修改活动状态为审核失败，
     * 3 - 无法修改已经通过的活动
     * }
     */
    public int checkActivity(int activityId, boolean checkedResult) {
        Optional<Activity> activityOptional = activityReaderMapper.findActivityByActivityId(activityId);
        if (!activityOptional.isPresent()) {
            return 1;
        }
        Activity activity = activityOptional.get();
        if (activity.getActivityState().compare(ActivityState.PASSED) >= 0) {
            return 3;
        }
        if (checkedResult) {
            activityWriterMapper.modifyActivityState(activity.getActivityId(), ActivityState.PASSED);
            return 0;
        } else {
            activityWriterMapper.modifyActivityState(activity.getActivityId(), ActivityState.FAILED);
            return 2;
        }
    }
}
